package com.attendance.www.hr.service.impl;

import java.util.List;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.attendance.www.hr.dao.mapper.MainOrganizationsMapper;

import com.attendance.www.hr.entitys.MainOrganizations;
import com.attendance.www.hr.entitys.MainOrganizationsExample;
import com.attendance.www.hr.service.IMainOrganizationsService;


@Service
public class MainOrganizationsServiceImpl implements IMainOrganizationsService{
	
	@Resource
	private MainOrganizationsMapper mainOrganizationsDao;

	/**
	 * 删除机构数据
	 */
	@Override
	public boolean deleteMainOrganizations() {
		int record=mainOrganizationsDao.deleteByExample(new MainOrganizationsExample());
	    if(record<=0) {
			return false;
		}
		return true;
	}
	
	/**
	 * 新增机构数据
	 */
	@Override
	public void saveMainOrganizations(List<MainOrganizations> mainOrganizationsList) {
		for (int j = 0; j < mainOrganizationsList.size(); j++) {
			if(StringUtils.isNotBlank(mainOrganizationsList.get(j).getOrganType())) {
				//如果为公司，清空上级编码和名称
				if(mainOrganizationsList.get(j).getOrganType().equals("Company")) {
					mainOrganizationsList.get(j).setSuperiorCode(null);
					mainOrganizationsList.get(j).setSuperiorName(null);
				}
			}
		    mainOrganizationsDao.insertSelective(mainOrganizationsList.get(j));
		}
	}
	
	/**
	 * 查询机构数据
	 */
	@Override
	public List<MainOrganizations> getMainOrganizationsList() {
		MainOrganizationsExample example=new MainOrganizationsExample();
		example.setOrderByClause("organ_code asc");
		List<MainOrganizations> mainOrganizationsList =mainOrganizationsDao.selectByExample(example);
		return mainOrganizationsList;
	}
}
